//
//  HJGitView.m
//  project
//
//  Created by hu on 2019/3/28.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJGitView.h"
#import "HJCollectionFlowLayout.h"
#import "HJGitCell.h"
#import "HJPayVC.h"

@interface HJGiftScrollView ()<GitViewDelegate>
@property (nonatomic,strong) UIScrollView * scrollView;
@end

@implementation HJGiftScrollView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        self.frame = frame;
        [self configScroll];
    }
    return self;
}

-(void)configScroll {
    UIScrollView * scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.bounds;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(self.width*2, self.height);
    scrollView.pagingEnabled = YES;
    scrollView.scrollEnabled = NO;
    scrollView.bounces = YES;
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.scrollView = scrollView;
    [self addSubview:scrollView];
    
    HJGitView * gitView = [[HJGitView alloc]init];
    gitView.frame = CGRectMake(0, 0, self.width, self.height);
    gitView.delegate = self;
    [scrollView addSubview:gitView];
    
    HJPayGitView * payGiftView =[[HJPayGitView alloc]init];
    payGiftView.frame = CGRectMake(self.width, self.height-250, self.width, 250);
    [scrollView addSubview:payGiftView];
}

-(void)sendGiftClickedWithBtn:(UIButton *)btn giftModel:(nonnull HJGiftListModel *)model {
    if (model.amount.integerValue==0) {
        [self.scrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
    }else {
        
    }
}


@end



@interface HJGitView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,CustomViewFlowLayoutDelegate>
/*
 *剩余数
 */
@property (weak, nonatomic) IBOutlet UILabel *remainingNum;
/*
 *送多少个礼物
 */
@property (weak, nonatomic) IBOutlet UILabel *num;
/*
 *分页控件
 */
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btmViewH;
@property (weak, nonatomic) IBOutlet UIView *btmView;
@property (weak, nonatomic) IBOutlet UIButton *btmViewSutractBtn;
@property (weak, nonatomic) IBOutlet UIButton *btmViewAddBtn;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

@property (weak, nonatomic) IBOutlet UIView *gitSubView;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray <HJGiftListModel* >* giftArray;
@property (nonatomic,strong) HJGiftListModel * listModel;
@end
static NSString * const identifier = @"GitCell";
@implementation HJGitView

-(instancetype)init {
    if (self == [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJGitView" owner:nil options:nil].firstObject;
        [self initSubViews];
    }
    return self;
}
-(void)initSubViews {
    self.pageControl.hidesForSinglePage = YES;
    self.btmViewH.constant = 0.0;
    self.btmView.hidden = YES;
    self.remainingNum.font = [UIFont systemFontOfSize:SCREEN_WIDTH/414*15];
    
    [self loadRequest];
}
-(NSMutableArray<HJGiftListModel *> *)giftArray {
    if (!_giftArray) {
        _giftArray = [NSMutableArray array];
    }
    return _giftArray;
}
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        HJCollectionFlowLayout * layout = [[HJCollectionFlowLayout alloc] initWithRowCount:2 itemCountPerRow:4];
        [layout setColumnSpacing:10 rowSpacing:10 edgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
        layout.delegate = self;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:@"HJGitCell" bundle:nil] forCellWithReuseIdentifier:identifier];
        [self.gitSubView addSubview:_collectionView];
    }
    return _collectionView;
}
-(void)loadRequest {
    NSString * url = [Api stringByAppendingString:GiftListApi];
    [HJHttpTool post:url params:@{@"userId":@(13)} success:^(HJResponeds *result) {
        [self.giftArray removeAllObjects];
        NSArray * arr =[NSArray yy_modelArrayWithClass:[HJGiftListModel class] json:result.data];
        [self.giftArray addObjectsFromArray:arr];
        [self.collectionView reloadData];
    } failure:^(id result) {
    }];
}

- (IBAction)pageTun:(UIPageControl *)sender {
    CGSize viewsize = self.collectionView.frame.size;
    CGRect rect = CGRectMake(sender.currentPage * viewsize.width, 0, viewsize.width, viewsize.height);
    [self.collectionView scrollRectToVisible:rect animated:YES];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = self.gitSubView.bounds;
}


// 加减礼物数
- (IBAction)addAndSubtractBtnClicked:(UIButton *)sender {
    NSInteger gitNum = self.num.text.integerValue;
    switch (sender.tag) {
        case 200:
            gitNum--;
            if (gitNum==0) return;
            break;
        case 201:
            gitNum++;
            break;
        default:
            break;
    }
    self.num.text = [NSString stringWithFormat:@"%zd",gitNum];
}

- (IBAction)sendGiftBtnClicked:(id)sender {
    if ([self.delegate respondsToSelector:@selector(sendGiftClickedWithBtn:giftModel:)]) {
        [self.delegate sendGiftClickedWithBtn:sender giftModel:self.listModel];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.giftArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGitCell * gitCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//    HJGiftListModel * model = self.giftArray[indexPath.row];
//    if (indexPath.row==0) {
//        model.isSelecte = YES;
//    }else {
//        model.isSelecte = NO;
//    }
    gitCell.giftModel = self.giftArray[indexPath.row];
    return gitCell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGitCell * giftCell = (HJGitCell *)[collectionView cellForItemAtIndexPath:indexPath];
    HJGiftListModel * model = self.giftArray[indexPath.row];
    model.isSelecte = YES;
    giftCell.giftModel = model;
    if (model.amount.integerValue==0) {
        self.btmViewAddBtn.hidden = YES;
        self.num.hidden = YES;
        self.btmViewSutractBtn.hidden = YES;
        self.remainingNum.text= @"您的礼物仓库还没有礼物";
        [self.sendBtn setTitle:@"购买" forState:0];
    }else {
        self.btmViewAddBtn.hidden = NO;
        self.btmViewSutractBtn.hidden = NO;
        self.num.hidden = NO;
        self.remainingNum.text = [NSString stringWithFormat:@"剩余%@",model.amount];
        [self.sendBtn setTitle:@"赠送" forState:0];
    }
    self.btmView.hidden = NO;
    self.btmViewH.constant = 50.0;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGitCell * giftCell = (HJGitCell *)[collectionView cellForItemAtIndexPath:indexPath];
    HJGiftListModel * model = self.giftArray[indexPath.row];
    model.isSelecte = NO;
    giftCell.giftModel = model;
}

#pragma mark --CustomViewFlowLayoutDelegate
-(void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout page:(NSInteger)page {
    self.pageControl.numberOfPages = page;
    NSInteger pages = collectionView.contentOffset.x/collectionView.width+0.5;
    self.pageControl.currentPage = pages;
}

@end


@implementation HJPayGitView


-(instancetype)init {
    if (self == [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJGitView" owner:nil options:nil].lastObject;
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    self.cycleBGView.layer.cornerRadius = 10;
    self.cycleBGView.layer.masksToBounds = YES;
    
    [self.payBtnBG rounded:UIRectCornerAllCorners radius:20];
    [self.payBtnBG setHorizontalGradinet:@[[UIColor colorWithHex:0xfe6a7f],[UIColor colorWithHex:0xff8980]]];
}


- (IBAction)addAndSubtractClicked:(UIButton *)sender {
    NSInteger gitNum = self.gift_Num.text.integerValue;
    switch (sender.tag) {
        case 2000:
            gitNum --;
            if (gitNum==0) return;
            break;
        case 2001:
            gitNum ++;
            break;
        default:
            break;
    }
    self.gift_Num.text = [NSString stringWithFormat:@"%zd",gitNum];
}


- (IBAction)payBtnClicked:(UIButton *)sender {
    HJPayVC * payVC = [[HJPayVC alloc]init];
    [[UIViewController currentViewController].navigationController pushViewController:payVC animated:YES];
}

@end



@implementation HJGiftListModel


@end
